//
//  WYEmoticonKeyBoard.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/25.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit
import SnapKit

//很显然 这里创建了一个 collectionView 它是由自己的代理和数据源方法 也就是说他可以成型 让后整体移出去 想使用的时候就可以调用
//对于其 cell 也可以自定义 那么这就跟家完美了

//定义全局变量 cell 标记
private var WYEmoticonCollectionViewCellIndentifier = "WYEmoticonCollectionViewCellIndentifier"
class WYEmoticonKeyBoard: UIView {
    
    //懒加载 选择按钮
    private lazy var toolBar: WYEmoticonToorBarView = {
        
        let view = WYEmoticonToorBarView(frame: CGRectZero)
        
        return view
    
    }()
    

    //懒加载 collectioView
    private lazy var emoticonCollectionView: UICollectionView = {
       
        let viewLayout = UICollectionViewFlowLayout()
        //滚动方向
        viewLayout.scrollDirection = .Horizontal
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: viewLayout)
//        view.backgroundColor = RandomColor()
        //设置页数
        view.pagingEnabled = true
        
        //关闭横竖滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        //关闭弹簧效果
        view.bounces = false
        //注册 cell
        view.registerClass(WYEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: WYEmoticonCollectionViewCellIndentifier)
        //设置数据源方法
        view.dataSource = self
        view.delegate = self
        return view
    }()
    //重写
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置布局
    private func setUpUI() {
    
//        backgroundColor = RandomColor()
        //添加控件
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        
        
        //添加约束
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(30)
        }
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self.toolBar.snp_top)
        }
        
        //MARK: -- 5 使用闭包 处理逻辑问题 区分点击的按钮是哪个按钮 
        toolBar.selectedButtonClosure = { [weak self](type: WYEmoticonToolBarButtonType) in
          
            //定义一个将要滚动到NSIndexpath
            let indexPath: NSIndexPath
            // MARK: -- 6 采用 switch 方式 确定每一项点击事件
            switch type {
                case .Normal:
                    print("normal")
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
                case .Emoji:
                    print("emoji")
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
                case .Lxh:
                    print("LXh")
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
                
            }
            //
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition:
                UICollectionViewScrollPosition.Left, animated: false)
        }
    }
    //View 的控件重新布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //获取表情视图的布局方式
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        //每项大小等于整个视图的大小
        flowLayout.itemSize = emoticonCollectionView.size
        //设置区域距离和区域内 item间距
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
    }
       
}
//延展 数据源方法
extension WYEmoticonKeyBoard: UICollectionViewDataSource , UICollectionViewDelegate{

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //工具数组的个数
        //return 3
        return WYEmoticonTools.shareTools.allEmticonArray.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //每个数组的里面的元素
//        return 2
        return WYEmoticonTools.shareTools.allEmticonArray[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WYEmoticonCollectionViewCellIndentifier, forIndexPath: indexPath) as! WYEmoticonCollectionViewCell
//        cell.backgroundColor = RandomColor()
        cell.backgroundColor = UIColor.whiteColor()
        cell.indexPath = indexPath
        cell.emoticons = WYEmoticonTools.shareTools.allEmticonArray[indexPath.section][indexPath.item]
        return  cell
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        print(scrollView.contentOffset.x)
        //获取屏幕显示Cell\  这里最多两个
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
           //从小 达大的排列
            return firstCell.x < secondCell.x
        }
        //判断哪个 cell 显示的多
        if cells.count == 2 {
            //第一个 第二个cell
            let firstCell = cells.first!
            let secondCell = cells.last!
            
            print(firstCell)
            print(secondCell)
            //第一个偏移量
            let firstCellContentOffSetX = firstCell.x - scrollView.contentOffset.x
            let secondCellContentOffSetX = secondCell.x - scrollView.contentOffset.x
            
            print(firstCellContentOffSetX)
            print(secondCellContentOffSetX)
            
            let cell: UICollectionViewCell
            //差值小 哪个就多
            if firstCellContentOffSetX < secondCellContentOffSetX {
            
                print("第一个")
                cell = firstCell
            } else {
            
                print("第二个")
                cell = secondCell
            }
            //3 通过cell获取对应的 indexPath
            let indexpath = emoticonCollectionView.indexPathForCell(cell)!
            //通过 indexPath 获取对应这个 section
            let section = indexpath.section
            
            toolBar.selectedButtonwithSection(section)
            
        }
        
    }
}
